package tuling.dubbo.client;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.codingvip.server.UserService;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;

public class YoungClient {
    //基于URL构建远程服务
    public UserService buildRemoteServices(String remoteUrl) {
        ReferenceConfig<UserService> referenceConfig = new ReferenceConfig();
        referenceConfig.setInterface(UserService.class);

        referenceConfig.setUrl(remoteUrl);
        referenceConfig.setApplication(new ApplicationConfig("young-app"));

        return referenceConfig.get();

    }

    public static void main(String[] args) throws IOException {
        YoungClient youngClient = new YoungClient();
        List<UserService> serviceList = new ArrayList<>();
        serviceList.add(youngClient.buildRemoteServices("dubbo://169.254.216.36:20880/com.codingvip.server.UserService?anyhost=true&application=simple-app&bind.ip=169.254.216.36&bind.port=20880&dubbo=2.6.2&generic=false&interface=com.codingvip.server.UserService&methods=listAllUser&pid=56815&side=provider&timestamp=1543498389939"));
        serviceList.add(youngClient.buildRemoteServices("dubbo://169.254.216.36:20881/com.codingvip.server.UserService?anyhost=true&application=simple-app&bind.ip=169.254.216.36&bind.port=20881&dubbo=2.6.2&generic=false&interface=com.codingvip.server.UserService&methods=listAllUser&pid=56928&side=provider&timestamp=1543498431389"));
        String cmd;
        int count = 0;
        while(!"exit".equals(cmd=read())){
            System.out.println((serviceList.get(count++ % serviceList.size())).listAllUser(980));
        }




    }

    private static String read() throws IOException {
        byte[] b = new byte[1024];
        LineNumberReader lineNumberReader = new LineNumberReader(new InputStreamReader(System.in));
        return lineNumberReader.readLine();
    }
}
